package com.teichmann;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.repackaged.com.google.common.base.StringUtil;
import com.teichmann.bo.Car;
import com.teichmann.bo.CarQueue;
import com.teichmann.engine.OnlineExecutor;
import com.teichmann.services.CarServices;

@SuppressWarnings("serial")
public class LoadQueueServlet extends HttpServlet {
	
	final String META_REFRESH = "<html><META HTTP-EQUIV=\"Refresh\" CONTENT=\"10\"><body>Refresh</body></html>";
	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

		execute(req, resp);
	}


	private void execute(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		
		if (request.getAttribute("firstTime") != null){
			response.getWriter().print(META_REFRESH);
			request.removeAttribute("firstTime");
		}
		else{
			
			List<CarQueue> cars = (List<CarQueue>) request.getSession().getAttribute("queue");
			
			if (cars.size() !=0){
				CarQueue carQueue =  cars.get(0);
				OnlineExecutor online = new OnlineExecutor(carQueue.getModelo());
				CarServices.updateQueueItem(carQueue.getId());
				
				cars.remove(carQueue);
			}
			
			
			
		}
		
		
	}

}
